package com.jucai.pppeiyouclient.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.CoinRecordAdapter;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.bean.CoinRecordBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.ToastUtils;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshForRecyclerView;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshLayout;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CoinRecordActivity extends BaseActivity implements PullToRefreshLayout.OnRefreshListener {
    private static final String TAG = "CoinRecordActivity :";
    private PullToRefreshLayout refreshLayout;
    private PullToRefreshForRecyclerView coinRecordsRv;
    private List<CoinRecordBean> coinRecords = new ArrayList<>();
    private CoinRecordAdapter recordAdapter;
    private Handler mHandler = new Handler();
    private int page = 1;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.common_activity_with_ptr);
        ((TextView) findViewById(R.id.common_title_text)).setText("金币记录");
        refreshLayout = (PullToRefreshLayout) findViewById(R.id.refresh_view);
        recordAdapter = new CoinRecordAdapter(CoinRecordActivity.this, coinRecords);
        coinRecordsRv = (PullToRefreshForRecyclerView) findViewById(R.id.content_view);
        coinRecordsRv.setLayoutManager(new LinearLayoutManager(CoinRecordActivity.this, LinearLayoutManager.VERTICAL, false));
        coinRecordsRv.setItemAnimator(new DefaultItemAnimator());
        coinRecordsRv.setAdapter(recordAdapter);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void loadNetData() {
        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Client_Wallet.getCoinLogs");
        param.put("page", "1");

        CommonRequestNetData.getJsonArrayData(CoinRecordActivity.this, param, true,new CommonRequestNetData.JsonArrayCallBack() {
            @Override
            public void parseJsonArray(JSONArray jsonArray) {
                LogUtils.e(TAG + "jsArr = " + jsonArray);
                coinRecords = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<CoinRecordBean>>() {
                }.getType());

                LogUtils.e(TAG + "coinRecords = " + coinRecords);
                recordAdapter.setData(coinRecords);
            }
        });
    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;

            case R.id.common_home_back:
                startActivity(new Intent(CoinRecordActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;
        }
    }

    @Override
    public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadNetData();
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }

    @Override
    public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
        ++page;
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                HashMap<String, String> param = new HashMap<>();
                param.put("service", "Client_Wallet.getCoinLogs");
                param.put("page", page + "");

                CommonRequestNetData.getJsonArrayData(CoinRecordActivity.this, param,true, new CommonRequestNetData.JsonArrayCallBack() {
                    @Override
                    public void parseJsonArray(JSONArray jsonArray) {
                        LogUtils.e(TAG + "jsArr = " + jsonArray);

                        List<CoinRecordBean> list = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<CoinRecordBean>>() {
                        }.getType());
                        if(list != null) {
                            if (!list.isEmpty()) {
                                coinRecords.addAll(list);
                            } else {
                                ToastUtils.showToast("没有更多数据！");
                            }
                        }
                        LogUtils.e(TAG + "coinRecords = " + coinRecords);
                        recordAdapter.setData(coinRecords);
                    }
                });
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }
}
